#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qstring.h>
#include <iostream>
#include <sstream>
#include <QMessageBox>
#include <QCryptographicHash>
using namespace std;

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
        //setup the UI and its signals
	ui->setupUi(this);
        connect(ui->pushButton_server, SIGNAL(clicked()), this, SLOT(makeServer()));
        connect(ui->pushButton_client, SIGNAL(clicked()), this, SLOT(makeClient()));
        ui->editor->setDisabled(true);

        Client = NULL;
		server = NULL;
}

MainWindow::~MainWindow()
{
    delete ui;
    ui = NULL;

    if (Client != NULL)
        delete Client;

	if (server != NULL)
		delete server;
}

void MainWindow::disableStuff() {
    ui->editor->setDisabled(false);
    ui->lineEdit_address->setDisabled(true);
    ui->lineEdit_port->setDisabled(true);
    ui->pushButton_client->setDisabled(true);
    ui->pushButton_server->setDisabled(true);
}

void MainWindow::makeServer() {
	server = new Server(ui->lineEdit_address->text(),ui->lineEdit_port->text().toInt());

	server->run();
}

void MainWindow::makeClient() {
    Client = new client(ui->editor);

    if (Client->connect(ui->lineEdit_address->text(), ui->lineEdit_port->text().toInt()))
        disableStuff();
}
